import requests
import execjs
from openpyxl import Workbook
from openpyxl.styles import Font,Alignment ,PatternFill


cookies = {
    'route': 'ef535e3a0d0b78922cdc0df3d6b94403',
    'yP4dbuxzYtffO': '60LSOse6kFtYR2BtSHfEh1Dq2c5GtwB9TOms3qUaYKtPoLgHYEYpGNJtm1iZrr2kWO_5HFXekQvKbkEjM7oh0X3A',
    'Hm_lvt_b966fe201514832da03dcf6cbf25b8a2': '1699240334',
    'Hm_lpvt_b966fe201514832da03dcf6cbf25b8a2': '1699240334',
    '__ts': '1699240333001',
    'BSFIT_EXPIRATION': '1699304811797',
    'BSFIT_DEVICEID': 'VPPwzyGZh5idLgQ5oadrH1SFy8UF1LOON95NzYx7fGDvgaG9lSNMwdIQ_yWV01E80uDOe6MmsGX2kvcn7ubStV21q-nupgnTDpSWCz3_2mrogoh5rEKB4JjvekD47dSNZBYGVJGh-QxEWmTub6VCV1oh6GOy7x1T',
    'BSFIT_89Axv': '31u561406x8MpjXw3g,3xvw313w3xDypD,3x8fpjXw31PLpw,3x8V3xkw31Pypg,3x8561kw3x8Q68',
    'yP4dbuxzYtffP': '02NVI8Oj68SivSsAU5Td9nuNgYHZMNnW.heU2FvSPgf7RV0sM0l4jwZ4vCZPe4zudldsg1SAcYTavIySb8vggCgRgAwZLxthE5tOmItXg1F.XoU.0QPOjs1Lg8yojH0o1t8nDDSyqnluB3jEhMf91nIYjmeCwdls4dW8INTELUWVf1Vr1J2FSUAPSHpSYMPAEhZ0.W54zDU7yNqdhM59hBbWlUQiibQpUrfEP6eaPkIhlkb6vPkh7hW6xS932nlDr3.hdCONeg_XgqXD.n4k4FPqs.CYIKqefVakF_VQTCyShZfcemkFT_sAL7NupQEbMbt_EITxgUTnAXGv43F4hzFPm1arxCishBts_k..g4Vref2WUiVjp4wBuHlLczF3kh1oCjcnElzAozzMWkkv_.kRK36t_gS1BSqdCnLAC5lq',
}

headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    # 'Cookie': 'route=ef535e3a0d0b78922cdc0df3d6b94403; yP4dbuxzYtffO=60LSOse6kFtYR2BtSHfEh1Dq2c5GtwB9TOms3qUaYKtPoLgHYEYpGNJtm1iZrr2kWO_5HFXekQvKbkEjM7oh0X3A; Hm_lvt_b966fe201514832da03dcf6cbf25b8a2=1699240334; Hm_lpvt_b966fe201514832da03dcf6cbf25b8a2=1699240334; __ts=1699240333001; BSFIT_EXPIRATION=1699304811797; BSFIT_DEVICEID=VPPwzyGZh5idLgQ5oadrH1SFy8UF1LOON95NzYx7fGDvgaG9lSNMwdIQ_yWV01E80uDOe6MmsGX2kvcn7ubStV21q-nupgnTDpSWCz3_2mrogoh5rEKB4JjvekD47dSNZBYGVJGh-QxEWmTub6VCV1oh6GOy7x1T; BSFIT_89Axv=31u561406x8MpjXw3g,3xvw313w3xDypD,3x8fpjXw31PLpw,3x8V3xkw31Pypg,3x8561kw3x8Q68; yP4dbuxzYtffP=02NVI8Oj68SivSsAU5Td9nuNgYHZMNnW.heU2FvSPgf7RV0sM0l4jwZ4vCZPe4zudldsg1SAcYTavIySb8vggCgRgAwZLxthE5tOmItXg1F.XoU.0QPOjs1Lg8yojH0o1t8nDDSyqnluB3jEhMf91nIYjmeCwdls4dW8INTELUWVf1Vr1J2FSUAPSHpSYMPAEhZ0.W54zDU7yNqdhM59hBbWlUQiibQpUrfEP6eaPkIhlkb6vPkh7hW6xS932nlDr3.hdCONeg_XgqXD.n4k4FPqs.CYIKqefVakF_VQTCyShZfcemkFT_sAL7NupQEbMbt_EITxgUTnAXGv43F4hzFPm1arxCishBts_k..g4Vref2WUiVjp4wBuHlLczF3kh1oCjcnElzAozzMWkkv_.kRK36t_gS1BSqdCnLAC5lq',
    'Pragma': 'no-cache',
    'Referer': 'https://ctbpsp.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

response = requests.get(
    'https://ctbpsp.com/cutominfoapi/recommand/type/5/pagesize/10/currentpage/5',
    cookies=cookies,
    headers=headers,
)
result_data = execjs.compile(open('招标公告逆向JS.js','r',encoding='utf-8').read()).call('decryptByDES',response.json())

# 创建工作簿
wb = Workbook()

# 创建工作表
ws = wb.active


headers = ['bulletinID','tenderProjectName','noticeName','noticeMedia','bulletinTypeName','docGetEndTime','bidDocRefferEndTime','bidOpenTime','notieIndustriestName','reginProvince','serverPlat','tradePlat','dataPlat','regionCode','regionName','noticeSendTime','bulletinSource','noticeUrl','superviseDeptName','leftOpenBidDay','platformCode','platformName','commentCount','hot','viewCount','favCount','subscriptionCount','tenderBidder','tenderAgency','isNew','grade','dataSource','potentialBidderDetails','classifyName','classifyCode','isBulletinChain','subcription','similarProjectsBidInfo','fav']

# 设置字体样式
font = Font(name='Calibri', size=14, bold=True, color='FFFFFF')

# 设置单元格样式
fill = PatternFill(fill_type='solid', start_color='1E90FF', end_color='ffffff')
# 设置第一行加粗
for col_num,header in enumerate(headers,1):
    cell = ws.cell(row=1,column=col_num,value=header)
    cell.font = font
    cell.alignment = Alignment(horizontal='center')
    cell.fill = fill
# for index,item in enumerate(result_data['data']['dataList']):
# ws.append(headers)

for row in result_data['data']['dataList']:
    row_data = [row.get(header, '') for header in headers]
    ws.append(row_data)

wb.save('data.xlsx')
